<?php
class Member extends AppModel {
	var $name = 'Member';

	var $primaryKey  = 'id';

	var $useTable = "member";

	var $validate = array(
		'username' => array(
			'alphaNumeric' => array(
				'rule' => 'alphaNumeric',
				'required' => true,
				'message' => 'Alphabets and numbers only'
			),
			'between' => array(
				'rule' => 'duplicateUser',
				'message' => 'Username exist. Please enter again'
			)
		),
		'passwd' => array(
			'rule' => array('minLength', '8'),
			'message' => 'Mimimum 8 characters long'
		),
		'email' => array(
			'email' => array(
				'rule' => 'email',
			),
			'between' => array(
				'rule' => 'duplicateMail',
				'message' => 'Email exist. Please enter again'
			)
		)
	);

	function beforeSave() {
		if(isset($this->data[$this->alias]['username'])) {
			$this->data[$this->alias]['username']
				= trim($this->data[$this->alias]['username']);
		}

		if(isset($this->data[$this->alias]['address'])) {
			$this->data[$this->alias]['address']
				= trim(strip_tags($this->data[$this->alias]['address']));
		}

		if(isset($this->data[$this->alias]['fullName'])) {
			$this->data[$this->alias]['fullName']
				= trim(strip_tags($this->data[$this->alias]['fullName']));
		}

		if(isset($this->data[$this->alias]['phone'])) {
			$this->data[$this->alias]['phone']
				= trim(strip_tags($this->data[$this->alias]['phone']));
		}

		if(isset($this->data[$this->alias]['mobile'])) {
			$this->data[$this->alias]['mobile']
				= trim(strip_tags($this->data[$this->alias]['mobile']));
		}

		if(isset($this->data[$this->alias]['identityCard'])) {
			$this->data[$this->alias]['identityCard']
				= trim(strip_tags($this->data[$this->alias]['identityCard']));
		}

		if(isset($this->data[$this->alias]['city'])) {
			$this->data[$this->alias]['city']
				= intval($this->data[$this->alias]['city']);
		}

		if(isset($this->data[$this->alias]['location'])) {
			$this->data[$this->alias]['location']
				= intval($this->data[$this->alias]['location']);
		}

		if(isset($this->data[$this->alias]['ipasswd'])) {
			$this->data[$this->alias]['passwd']
				= Security::hash(trim($this->data[$this->alias]['ipasswd']), null, true);
		}
		return true;
	}

	function loadJoin() {
		$this->bindModel(
			 array(
			 	'belongsTo' => array (
					'Invite' => array(
						'className' => 'Member',
						'foreignKey' => 'invite'
					)
				)
			),
			false
		);
	}



	/**
	 * Check duplicate username ...
	 * @return boolean
	 */
	function duplicateUser() {
		$has = $this->find(
			'count',
			array(
				'conditions' => array(
					'username' => trim($this->data[$this->name]['username'])
				)
			)
		);

		if ($has > 0) {
			return false;
		}

		return true;
	}

	function duplicateMail() {
		$has = $this->find(
			'count',
			array(
				'conditions' => array(
					'email' => trim($this->data[$this->name]['email'])
				)
			)
		);

		if ($has > 0) {
			return false;
		}

		return true;
	}

	/**
	 * Get user by username ...
	 * @param string $user
	 * @param string $type
	 * @return NULL|Ambigous <multitype:, NULL, boolean, mixed>
	 */
	function byUsername($user, $type = 'first') {
		if(empty($user)) return null;
		//get user
		return $this->find(
			$type,
			array(
				'conditions'	=> array('username'=>$user)
			)
		);

	}

	/**
	 * Get user by email ...
	 * @param string $email
	 * @param string $type
	 * @return Ambigous <multitype:, NULL, boolean, mixed>
	 */
	function byEmail($email, $type = 'first') {
		//get email
		return $this->find(
			$type,
			array(
				'conditions'	=> array('email'=>$email)
			)
		);

	}

	/**
	 * Get by key ...
	 * @param integer $id
	 * @param string $type
	 * @return Ambigous <multitype:, NULL, boolean, mixed>
	 */
	function get($id, $type = 'first') {
		return $this->find(
			$type,
			array(
				'conditions'	=> array(
					"{$this->alias}.id" => $id
				)
			)
		);
	}

}
?>